package com.supply.dl.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.supply.dl.R;
import com.supply.dl.app.ApiService;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.adapter.NormalAdapter;
import com.supply.dl.app.adapter.NormalPositionAdapter;
import com.supply.dl.app.adapter.RecyclerViewDivider;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.app.http.BaseObserver;
import com.supply.dl.app.http.HttpMethod;
import com.supply.dl.app.http.RxSchedulers;
import com.supply.dl.bean.JobAttachBean;
import com.supply.dl.bean.ReceiveJobDetail;
import com.supply.dl.bean.ReceiveSaveJobPost;
import com.supply.dl.bean.SearchStockBean;
import com.supply.dl.bean.WorkNotesBean;
import com.supply.dl.utils.BigDecimalUtil;
import com.supply.dl.utils.DialogUtils;
import com.supply.dl.utils.FilePostUtil;
import com.supply.dl.utils.PictureHelper;
import com.supply.dl.utils.StringUtils;
import com.supply.dl.utils.Utils;
import com.supply.dl.utils.utils.ToastUtil;
import com.supply.dl.view.MaxRecyclerView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 领料收货详情页
 * <p>
 * Author: tian <br>
 * Time: 2018/6/12 15:20
 */
public class PickCollectGoodsActivity extends BaseActivity {

    /**
     * 保存时候的提交对象
     */
    private ReceiveSaveJobPost receiveSaveJobPost;

    /**
     * 物料列表
     */
    @BindView(R.id.materail_rc)
    MaxRecyclerView materail_rc;

    /**
     * 附件列表
     */
    @BindView(R.id.fujian_rc)
    MaxRecyclerView fujian_rc;

    /**
     * 物料横向列表 适配器
     */
    private NormalAdapter<ReceiveJobDetail.NoticeOrderDetailInfo> materielAdapter;

    /**
     * 数据源
     */
    private List<ReceiveJobDetail.NoticeOrderDetailInfo> itemList, hasDoList, unDoList;

    /**
     * 当前选中的物料
     */
    private ReceiveJobDetail.NoticeOrderDetailInfo currentShowMaterier;

    /**
     * 为定位
     */

    @BindView(R.id.un_location_tv)
    TextView notLocatedTv;

    @BindView(R.id.unlocation_line)
    View notLocatedLineView;

    /**
     * 已定位
     */
    @BindView(R.id.located_tv)
    TextView hadLocatedTv;

    @BindView(R.id.had_location_line)
    View hadLocatedLineView;

    /**
     * 保存
     */
    @BindView(R.id.add_location_tv)
    TextView addNewPositionBtn;

    /**
     * 过账
     */
    @BindView(R.id.location_lot_tv)
    TextView batchPositionBtn;

    /**
     * 定位列表
     */
    @BindView(R.id.location_rc)
    MaxRecyclerView positionRecyclerView;

    /**
     * 收缩和展开按钮
     */
    @BindView(R.id.open_hide_img)
    ImageView open_hide_img;

    @BindView(R.id.add_work_detail_img)
    ImageView add_work_detail_img;

    /**
     * 作业数据
     */
    private ArrayList<ReceiveJobDetail.RukuNoticeOrderDoneInfo> workDatas;

    /**
     * 在点击 展示或者隐藏数据按钮时候，使用的数据
     */
    private ArrayList<ReceiveJobDetail.RukuNoticeOrderDoneInfo> tempDatas;

    /**
     * 附件数据
     */
    private ArrayList<JobAttachBean> fujianDatas;

    /**
     * 定位列表适配器
     */
    private NormalPositionAdapter<ReceiveJobDetail.RukuNoticeOrderDoneInfo> positionAdapter;

    /**
     * 附件适配器
     */
    private NormalAdapter<JobAttachBean> fujianAapter;

    /**
     * 领料收货详情源数据
     */
    private ReceiveJobDetail receiveJobDetail;

    /**
     * 通知单id
     */
    private String id;

    /**
     * 类型
     */
    @BindView(R.id.type_tv)
    TextView type_tv;

    /**
     * 领料单号
     */
    @BindView(R.id.pick_goods_id_tv)
    TextView pick_goods_id_tv;

    /**
     * 收货仓库
     */
    @BindView(R.id.get_ck_tv)
    TextView get_ck_tv;

    /**
     * 可收货数量
     */
    @BindView(R.id.can_collect_count_tv)
    TextView can_collect_count_tv;

    /**
     * 物料编码
     */
    @BindView(R.id.wuliao_code_tv)
    TextView wuliao_code_tv;

    /**
     * 物料名字
     */
    @BindView(R.id.wuliao_name_tv)
    TextView wuliao_name_tv;

    /**
     * 实发数量
     */
    @BindView(R.id.send_count_tv)
    TextView send_count_tv;

    /**
     * 实收数量
     */
    @BindView(R.id.get_count_tv)
    TextView get_count_tv;

    /**
     * 作业记录状态
     */
    @BindView(R.id.operate_note_status_tv)
    TextView operate_note_status_tv;

    /**
     * 当前的作业item项
     */
    private ReceiveJobDetail.RukuNoticeOrderDoneInfo currentPostionItem;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_pick_collect_goods;
    }

    @Override
    protected void initView() {
        super.initView();
        showLeftBack();
        setTitleShow("领料收货详情");

        setRightHomeClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showToast(PickCollectGoodsActivity.this, "home");
            }
        });

        setRightScanClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showToast(PickCollectGoodsActivity.this, "scan");
                Utils.startScanActivity(PickCollectGoodsActivity.this);
            }
        });

        id = getIntent().hasExtra("id") ? getIntent().getStringExtra("id") : "";

        getReceiveJobDetail(id);
    }

    @Override
    protected void initLoadingView() {
        super.initLoadingView();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        itemList = new ArrayList<>();
        hasDoList = new ArrayList<>();
        unDoList = new ArrayList<>();

        workDatas = new ArrayList<>();
        tempDatas = new ArrayList<>();

        receiveSaveJobPost = new ReceiveSaveJobPost();

        //对于多个数据需要处理,用于做展示和收缩
        if (workDatas.size() > 1) {
            for (int i = 1; i < workDatas.size(); i++) {
                tempDatas.add(workDatas.get(i));
            }

            ReceiveJobDetail.RukuNoticeOrderDoneInfo tempBean = workDatas.get(0);
            workDatas.clear();
            workDatas.add(tempBean);
        }

        notLocatedTv.setText("未作业");
        hadLocatedTv.setText("已作业");

    }

    @Override
    protected void initRecyclerView() {

        //初始化物料列表
        materielAdapter = new NormalAdapter<ReceiveJobDetail.NoticeOrderDetailInfo>(this, itemList) {
            @Override
            protected void convert(ViewHolder holder, ReceiveJobDetail.NoticeOrderDetailInfo data) {
                TextView name_tv = holder.getView(R.id.material_position_info_item_textView);
                if (data.isSelect()) {
                    name_tv.setBackgroundResource(R.drawable.shape_search_btn_blue_bg);
                    name_tv.setTextColor(getResources().getColor(R.color.whiteColor));
                }
                //
                else {
                    name_tv.setBackgroundResource(R.drawable.shape_solid_gray_bg);
                    name_tv.setTextColor(getResources().getColor(R.color.textColor));
                }
                name_tv.setText(data.getSkuName());
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.materiel_position_info_item;
            }
        };

        materielAdapter.setOnItemClickListener(new NormalAdapter.OnItemClickListener<ReceiveJobDetail.NoticeOrderDetailInfo>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, ReceiveJobDetail.NoticeOrderDetailInfo data, int position) {

                for (ReceiveJobDetail.NoticeOrderDetailInfo item : itemList) {
                    item.setSelect(false);
                }

                data.setSelect(true);

                materielAdapter.notifyDataSetChanged();
                fillMaterialView();
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        materail_rc.setLayoutManager(linearLayoutManager);
        materail_rc.setAdapter(materielAdapter);

        //初始化
        positionAdapter = new NormalPositionAdapter<ReceiveJobDetail.RukuNoticeOrderDoneInfo>(this, workDatas) {
            @Override
            protected void convert(ViewHolder holder, final ReceiveJobDetail.RukuNoticeOrderDoneInfo data, int index) {

                holder.setText(R.id.work_info_item_workOrderNumberTextView, "作业" + (index + 1));

                TextView select_stock_tv = holder.getView(R.id.work_info_item_showReservoirPositionTv);
                select_stock_tv.setText(data.getTfInStockName());

                final EditText amount_et = holder.getView(R.id.work_info_item_showNumberForPieceTv);
                final EditText weight_et = holder.getView(R.id.work_info_item_showWeightForTonTv);
                final EditText volume_et = holder.getView(R.id.work_info_item_showVolumeForSquareTv);
                final EditText remark_et = holder.getView(R.id.work_info_item_showRemarksTv);

                amount_et.setText(currentShowMaterier.getTfJobType() == 0 ? data.getTfInAmout() + "" : data.getTfOutAmout() + "");
                weight_et.setText(currentShowMaterier.getTfJobType() == 0 ? data.getTfInWeight() + "" : data.getTfOutWeight() + "");
                volume_et.setText(currentShowMaterier.getTfJobType() == 0 ? data.getTfInVolume() + "" : data.getTfOutVolume() + "");

                holder.setText(R.id.work_info_item_showInventoryStatusTv, data.getStockStatusName());
                holder.setText(R.id.work_info_item_showInventoryTypeTv, data.getStockTypeName());
                holder.setText(R.id.work_info_item_showStandardTv, data.getUnitName());
                holder.setText(R.id.work_info_item_showBatchNumberTv, data.getBatchNo());
                holder.setText(R.id.work_info_item_showRemarksTv, data.getRemark());

                //已作业信息 不允许修改
                if (currentShowMaterier.getTfJobType() == 0) {
                    editTextable(amount_et, false);
                    editTextable(weight_et, false);
                    editTextable(volume_et, false);
                    editTextable(remark_et, false);
                }
                //
                else {
                    editTextable(amount_et, true);
                    editTextable(weight_et, true);
                    editTextable(volume_et, true);
                    editTextable(remark_et, true);
                }


                amount_et.setTag(data.getCreateTime());
                weight_et.setTag(data.getCreateTime());
                volume_et.setTag(data.getCreateTime());
                remark_et.setTag(data.getCreateTime());

                amount_et.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        if (!StringUtils.isCanUse(s.toString()))
                            return;

                        if (data.getCreateTime().equals(amount_et.getTag()))
                            return;

                        currentPostionItem = data;
                        dealAmoutInput(BigDecimalUtil.getBigDecimalByString(s.toString()), amount_et);
                    }
                });
                weight_et.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        if (!StringUtils.isCanUse(s.toString()))
                            return;
                        if (data.getCreateTime().equals(amount_et.getTag()))
                            return;

                        currentPostionItem = data;
                        dealWeightInput(BigDecimalUtil.getBigDecimalByString(s.toString()), weight_et);
                    }
                });
                volume_et.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        if (!StringUtils.isCanUse(s.toString()))
                            return;
                        if (data.getCreateTime().equals(amount_et.getTag()))
                            return;

                        currentPostionItem = data;
                        dealVolumeInput(BigDecimalUtil.getBigDecimalByString(s.toString()), volume_et);
                    }
                });
                remark_et.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        if (!StringUtils.isCanUse(s.toString()))
                            return;

                        if (data.getCreateTime().equals(amount_et.getTag()))
                            return;

                        currentPostionItem = data;
                        data.setRemark(s.toString());
                    }
                });

                select_stock_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        //判断当前是否是 已作业，已作业不允许修改
                        if (currentShowMaterier.getTfJobType() == 0) {
                            Toast.makeText(PickCollectGoodsActivity.this, "已作业数据不允许修改", Toast.LENGTH_SHORT).show();
                            return;
                        }

                        Bundle bundle = new Bundle();
                        //物料编码
                        bundle.putString("skuId", currentShowMaterier.getSkuCode());
                        //页面名字
                        bundle.putString("titleName", "库位选择");
                        //仓库id
                        bundle.putString("whId", "");

                        Intent it = new Intent(PickCollectGoodsActivity.this, DepotSelectActivity.class);
                        it.putExtras(bundle);
                        startActivityForResult(it, AppConfig.BaseDataAction.REQCODE_CHOOSE_STOCK);
                        currentPostionItem = data;
                    }
                });
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.pick_collect_goods_item;
            }
        };

        positionRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        positionRecyclerView.setAdapter(positionAdapter);

        fujianDatas = new ArrayList<>();
        fujianAapter = new NormalAdapter<JobAttachBean>(this, fujianDatas) {
            @Override
            protected void convert(ViewHolder holder, final JobAttachBean data) {

                String[] temp = data.photoPath.split("/");
                holder.setText(R.id.fujian_name_tv, temp[temp.length - 1]);
                holder.getView(R.id.delete_fujian_img).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtils.showDialog(PickCollectGoodsActivity.this, "提示", "确定删除当前附件吗?", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                fujianDatas.remove(data);
                                fujianAapter.notifyDataSetChanged();
                                receiveSaveJobPost.getJobAttachList().remove(data);
                            }
                        }).show();
                    }
                });
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.item_fujian;
            }
        };

        fujian_rc.setLayoutManager(new LinearLayoutManager(this));
        fujian_rc.addItemDecoration(new RecyclerViewDivider(this, LinearLayoutManager.VERTICAL));
        fujian_rc.setAdapter(fujianAapter);

        isHadLocation(false);

    }

    @OnClick({R.id.un_location_tv, R.id.located_tv, R.id.add_location_tv, R.id.location_lot_tv,
            R.id.open_hide_img, R.id.add_work_detail_img, R.id.add_fujian_img, R.id.work_remark_ly})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

            case R.id.work_remark_ly:
                Intent intent = new Intent(PickCollectGoodsActivity.this, OperateRemarkActivity.class);
                intent.putExtra("factoryId", "");

                PickCollectGoodsActivity.this.startActivityForResult(intent, AppConfig.BaseDataAction.REQCODE_ADD_OPERATE_REMARK);
                break;

            case R.id.un_location_tv://未定位
                isHadLocation(false);
                break;
            case R.id.located_tv://已定位
                isHadLocation(true);
                break;
            case R.id.add_location_tv://添加新定位

                if (receiveSaveJobPost.isHasPost()) {
                    Toast.makeText(this, "数据已经保存", Toast.LENGTH_SHORT).show();
                    return;
                }

                saveJob(false);
                break;
            case R.id.location_lot_tv://批量定位
                if ("已经过账".equals(batchPositionBtn.getText())) {
                    Toast.makeText(this,
                            "已经过账", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (receiveSaveJobPost.isHasPost()) {
                    suerReceiveJobs();
                }
                //
                else {
                    saveJob(true);
                }
                break;

            //向上或者向下的 按钮
            case R.id.open_hide_img:

                //展示
                if (tempDatas.size() > 0) {
                    workDatas.addAll(tempDatas);
                    tempDatas.clear();
                    positionAdapter.notifyDataSetChanged();
                    open_hide_img.setBackgroundResource(R.mipmap.icon_up);
                    return;
                }

                //收缩
                if (workDatas.size() > 1) {
                    for (int i = 1; i < workDatas.size(); i++) {
                        tempDatas.add(workDatas.get(i));
                    }

                    ReceiveJobDetail.RukuNoticeOrderDoneInfo tempBean = workDatas.get(0);
                    workDatas.clear();
                    workDatas.add(tempBean);
                    positionAdapter.notifyDataSetChanged();

                    open_hide_img.setBackgroundResource(R.mipmap.icon_down);
                    return;
                }

                break;

            //添加作业明细
            case R.id.add_work_detail_img:

                if (currentShowMaterier == null) {
                    Toast.makeText(this, "没有物料不能进行添加新作业操作", Toast.LENGTH_SHORT).show();
                    return;
                }

                currentPostionItem = null;

                Bundle bundle = new Bundle();
                //物料编码
                bundle.putString("skuId", currentShowMaterier.getSkuCode());
                //页面名字
                bundle.putString("titleName", "库位选择");
                //仓库id
                bundle.putString("whId", "");

                Intent it = new Intent(PickCollectGoodsActivity.this, DepotSelectActivity.class);
                it.putExtras(bundle);
                startActivityForResult(it, AppConfig.BaseDataAction.REQCODE_CHOOSE_STOCK);

                break;

            //添加附件
            case R.id.add_fujian_img:
                PictureHelper.toSelectPicture(PickCollectGoodsActivity.this);
                break;
        }
    }

    /**
     * 是否定位
     *
     * @param isLocation
     */
    private void isHadLocation(boolean isLocation) {
        if (!isLocation) {
            notLocatedLineView.setVisibility(View.VISIBLE);
            hadLocatedLineView.setVisibility(View.INVISIBLE);
            notLocatedTv.setTextColor(getResources().getColor(R.color.blue));
            hadLocatedTv.setTextColor(getResources().getColor(R.color.color_gray));
            add_work_detail_img.setVisibility(View.VISIBLE);

        } else {
            hadLocatedLineView.setVisibility(View.VISIBLE);
            notLocatedLineView.setVisibility(View.INVISIBLE);
            batchPositionBtn.setVisibility(View.VISIBLE);
            hadLocatedTv.setTextColor(getResources().getColor(R.color.blue));
            notLocatedTv.setTextColor(getResources().getColor(R.color.color_gray));
            add_work_detail_img.setVisibility(View.INVISIBLE);
        }

        itemList.clear();
        itemList.addAll(isLocation ? hasDoList : unDoList);
        materielAdapter.notifyDataSetChanged();

        fillMaterialView();
    }

    /**
     * 获取领料详情数据
     *
     * @param id
     */
    private void getReceiveJobDetail(String id) {
        ApiService api = HttpMethod.getInstance().create(ApiService.class);
        api.receiveJobDetailForNew(id).compose(RxSchedulers.<BaseInfo<ReceiveJobDetail>>compose(this))
                .subscribe(new BaseObserver<ReceiveJobDetail>() {
                    @Override
                    public void onSuccess(BaseInfo<ReceiveJobDetail> baseInfo) {
                        if (baseInfo != null) {
                            receiveJobDetail = baseInfo.getData();
                            fillTopShow();
                            fillRecylerView();
                            fillMaterialView();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                    }
                });
    }


    private void fillTopShow() {
        type_tv.setText(receiveJobDetail.getOrderStatusName());
        pick_goods_id_tv.setText("领料单号:" + receiveJobDetail.getNoticeOrderNo());
        get_ck_tv.setText("入库车间:" + receiveJobDetail.getInWorkshopName());
        can_collect_count_tv.setText("实发总数量:" + receiveJobDetail.getSendTotalAmount() + "件 " +
                receiveJobDetail.getSendTotalWeight() + "吨 " + receiveJobDetail.getSendTotalVolume() + "方");
    }

    /**
     * 填充recylerview
     */
    private void fillRecylerView() {

        for (ReceiveJobDetail.NoticeOrderDetailInfo info : receiveJobDetail.getStoTfNoticeOrderAppRcvDetailDtos()) {
            if (info.getTfJobType() == 0)
                hasDoList.add(info);
            else
                unDoList.add(info);
        }

        if (hasDoList.size() > 0)
            hasDoList.get(0).setSelect(true);

        if (unDoList.size() > 0)
            unDoList.get(0).setSelect(true);

        //默认显示未作业
        itemList.addAll(unDoList);
        materielAdapter.notifyDataSetChanged();
    }

    /**
     * 填充物料显示布局
     */
    private void fillMaterialView() {

        if (itemList.size() == 0) {
            wuliao_code_tv.setText("");
            wuliao_name_tv.setText("");
            send_count_tv.setText("");
            get_count_tv.setText("");

            currentShowMaterier = null;
            workDatas.clear();
            positionAdapter.notifyDataSetChanged();
            return;
        }

        for (ReceiveJobDetail.NoticeOrderDetailInfo item : itemList) {
            if (item.isSelect()) {
                currentShowMaterier = item;
                break;
            }
        }

        wuliao_code_tv.setText("物料编码:" + currentShowMaterier.getSkuCode());
        wuliao_name_tv.setText("物料名称:" + currentShowMaterier.getSkuName());
        send_count_tv.setText("实发数量:" + currentShowMaterier.getSendTotalAmountD() + "件 " +
                currentShowMaterier.getSendTotalWeightD() + "吨 " + currentShowMaterier.getSendTotalAmountD() + "方");
        get_count_tv.setText("实收数量:" + currentShowMaterier.getRevTotalAmountD() + "件 " +
                currentShowMaterier.getRevTotalWeightD() + "吨 " + currentShowMaterier.getRevTotalVolumeD() + "方");

        workDatas.clear();
        workDatas.addAll(currentShowMaterier.getStoTfNoticeOrderAppRcvJobDtos());
        positionAdapter.notifyDataSetChanged();
        currentPostionItem = null;
    }

    private void dealAmoutInput(BigDecimal input, EditText editText) {
        if (currentShowMaterier.getSendTotalAmountD() == null || currentShowMaterier.getSendTotalAmountD().floatValue() == 0) {
            editText.setText("");
            editText.setHint("不以件计量，请勿输入");
            return;
        }

        if (input.floatValue() > currentShowMaterier.getSendTotalAmountD().floatValue()) {
            editText.setText("");
            editText.setHint("不能超过实发数量");
            return;
        }

        //已作业情况下
        if (currentShowMaterier.getTfJobType() == 0) {
            currentPostionItem.setTfInAmout(input);
        }
        //
        else {
            currentPostionItem.setTfOutAmout(input);
        }
    }

    private void dealWeightInput(BigDecimal input, EditText editText) {
        if (currentShowMaterier.getSendTotalWeightD() == null || currentShowMaterier.getSendTotalWeightD().floatValue() == 0) {
            editText.setText("");
            editText.setHint("不以吨计量，请勿输入");
            return;
        }

        if (input.floatValue() > currentShowMaterier.getSendTotalWeightD().floatValue()) {
            editText.setText("");
            editText.setHint("不能超过实发数量");
            return;
        }

        //已作业情况下
        if (currentShowMaterier.getTfJobType() == 0) {
            currentPostionItem.setTfInWeight(input);
        }
        //
        else {
            currentPostionItem.setTfOutWeight(input);
        }
    }

    private void dealVolumeInput(BigDecimal input, EditText editText) {
        if (currentShowMaterier.getSendTotalVolumeD() == null || currentShowMaterier.getSendTotalVolumeD().floatValue() == 0) {
            editText.setText("");
            editText.setHint("不以方计量，请勿输入");
            return;
        }

        if (input.floatValue() > currentShowMaterier.getSendTotalVolumeD().floatValue()) {
            editText.setText("");
            editText.setHint("不能超过实发数量");
            return;
        }

        //已作业情况下
        if (currentShowMaterier.getTfJobType() == 0) {
            currentPostionItem.setTfInVolume(input);
        }
        //
        else {
            currentPostionItem.setTfOutVolume(input);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null)
            return;
        switch (requestCode) {

            //选择库存
            case AppConfig.BaseDataAction.REQCODE_CHOOSE_STOCK:
                if (data.hasExtra("selectBean")) {
                    SearchStockBean selectBean = (SearchStockBean) data.getSerializableExtra("selectBean");
                    dealSelectStockResult(selectBean);
                }
                break;

            //添加作业记录
            case AppConfig.BaseDataAction.REQCODE_ADD_OPERATE_REMARK:
                ArrayList<WorkNotesBean> remarks = (ArrayList<WorkNotesBean>) data.getSerializableExtra("remarks");

                if (remarks != null && remarks.size() > 0) {
                    receiveSaveJobPost.setJobAmountList(remarks);
                    operate_note_status_tv.setText("已填写");
                }

                break;

            case PictureConfig.CHOOSE_REQUEST:
                // 图片、视频、音频选择结果回调
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);

                if (selectList == null || selectList.size() == 0)
                    return;

                ArrayList<JobAttachBean> photos = new ArrayList<>();
                for (LocalMedia media : selectList) {
                    JobAttachBean jobAttachBean = new JobAttachBean();
                    jobAttachBean.photoPath = media.getPath();
                    photos.add(jobAttachBean);
                }


                if (receiveSaveJobPost.getJobAttachList() == null)
                    receiveSaveJobPost.setJobAttachList(new ArrayList<JobAttachBean>());
                receiveSaveJobPost.getJobAttachList().addAll(photos);

                fujianDatas.clear();
                fujianDatas.addAll(receiveSaveJobPost.getJobAttachList());
                fujianAapter.notifyDataSetChanged();
                break;
        }
    }


    /**
     * 处理库存选择
     *
     * @param selectBean
     */
    private void dealSelectStockResult(SearchStockBean selectBean) {

        changeBtnShow(false);

        //新整
        if (currentPostionItem == null) {
            ReceiveJobDetail.RukuNoticeOrderDoneInfo info = new ReceiveJobDetail.RukuNoticeOrderDoneInfo();

            info.setRelatedJobId("");
            info.setStockType(selectBean.getStockType());
            info.setStockTypeName(selectBean.getStockTypeName());
            info.setStockType(selectBean.getStockType());
            info.setStockTypeName(selectBean.getStockTypeName());
            info.setUnitId(selectBean.getUnitId());
            info.setUnitName(selectBean.getUnitName());

            //生产班次
            info.setProductTeam("");
            info.setPackageWeight(selectBean.getPackageWeight());
            info.setBatchNo(selectBean.getBatchNo());
            info.setTfInStockId(selectBean.getStockId());
            info.setTfInStockName(selectBean.getStockName());
            info.setTfInWhId(selectBean.getWhId());
            info.setTfInWhName(selectBean.getWhName());
            info.setRemark(selectBean.getRemark());

            workDatas.add(info);
            currentShowMaterier.getStoTfNoticeOrderAppRcvJobDtos().add(info);
        }
        //修改业务
        else {
            currentPostionItem.setStockType(selectBean.getStockType());
            currentPostionItem.setStockTypeName(selectBean.getStockTypeName());
            currentPostionItem.setStockType(selectBean.getStockType());
            currentPostionItem.setStockTypeName(selectBean.getStockTypeName());
            currentPostionItem.setUnitId(selectBean.getUnitId());
            currentPostionItem.setUnitName(selectBean.getUnitName());

            currentPostionItem.setPackageWeight(selectBean.getPackageWeight());
            currentPostionItem.setBatchNo(selectBean.getBatchNo());
            currentPostionItem.setTfInStockId(selectBean.getStockId());
            currentPostionItem.setTfInStockName(selectBean.getStockName());
            currentPostionItem.setTfInWhId(selectBean.getWhId());
            currentPostionItem.setRemark(selectBean.getRemark());
            currentPostionItem.setTfInWhName(selectBean.getWhName());
        }

        positionAdapter.notifyDataSetChanged();
    }

    /**
     * 设置 EditText输入状态
     *
     * @param editText
     * @param editable
     */
    private void editTextable(EditText editText, boolean editable) {
        if (!editable) { // disable editing password
            editText.setFocusable(false);
            editText.setFocusableInTouchMode(false); // user touches widget on phone with touch screen
            editText.setClickable(false); // user navigates with wheel and selects widget
        } else { // enable editing of password
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            editText.setClickable(true);
        }
    }

    /**
     * 保存领料收货信息
     *
     * @param isNeedPassBill 是否需要过账
     */
    private void saveJob(final boolean isNeedPassBill) {
        //对数据的进行封装
        receiveSaveJobPost.setNoticeOrderId(receiveJobDetail.getNoticeOrderId());
        receiveSaveJobPost.setNoticeOrderNo(receiveJobDetail.getNoticeOrderNo());

        ArrayList<ReceiveSaveJobPost.JobDetail> jobs = new ArrayList<>();

        for (ReceiveJobDetail.NoticeOrderDetailInfo item : receiveJobDetail.getStoTfNoticeOrderAppRcvDetailDtos()) {
            if (item.getStoTfNoticeOrderAppRcvJobDtos() == null
                    || item.getStoTfNoticeOrderAppRcvJobDtos().size() == 0)
                continue;

            for (ReceiveJobDetail.RukuNoticeOrderDoneInfo operateItem : item.getStoTfNoticeOrderAppRcvJobDtos()) {
                ReceiveSaveJobPost.JobDetail job = new ReceiveSaveJobPost.JobDetail();

                job.setOrderId(receiveJobDetail.getTfOrderId());
                job.setOrderNo(receiveJobDetail.getTfOrderNo());
                job.setOrderDetailId(receiveJobDetail.getTfOrderDetailId());
                job.setNoticeOrderId(receiveJobDetail.getNoticeOrderId());
                job.setNoticeOrderNo(receiveJobDetail.getNoticeOrderNo());
                job.setNoticeOrderDetailId(receiveJobDetail.getTfNoticeOrderDetailId());

                job.setRelatedJobId(operateItem.getRelatedJobId());
                job.setJobType(item.getTfJobType());
                job.setIsActual(1);

                job.setActualStockId(operateItem.getTfInStockId());
                job.setActualWhId(operateItem.getTfInWhId());
                job.setPackageWeight(BigDecimalUtil.getBigDecimalByString(operateItem.getPackageWeight()));

                job.setSkuId(item.getSkuId());
                job.setSkuCode(item.getSkuCode());
                job.setSkuName(item.getSkuName());

                job.setActualAmount(item.getTfJobType() == 0 ? operateItem.getTfInAmout() : operateItem.getTfOutAmout());
                job.setActualWeight(item.getTfJobType() == 0 ? operateItem.getTfInWeight() : operateItem.getTfOutWeight());
                job.setActualVolume(item.getTfJobType() == 0 ? operateItem.getTfInVolume() : operateItem.getTfOutVolume());

                job.setStockStatus(operateItem.getStockStatus());
                job.setStockType(operateItem.getStockType());
                job.setUnitId(operateItem.getUnitId());
                job.setBatchNo(operateItem.getBatchNo());
                job.setRemark(operateItem.getRemark());

                jobs.add(job);
            }
        }

        receiveSaveJobPost.setAppJobDtoLists(jobs);

        if (receiveSaveJobPost.getJobAttachList() != null && receiveSaveJobPost.getJobAttachList().size() > 0)
            //是否有图片
            FilePostUtil.postFile(PickCollectGoodsActivity.this, receiveSaveJobPost.getJobAttachList(), new FilePostUtil.OnFilePostEndListener() {
                @Override
                public void onFilePostEnd() {
                    postData(isNeedPassBill);
                }
            });
        else
            postData(isNeedPassBill);
    }

    /**
     * 保存领料收货信息
     */
    private void postData(final boolean isNeedPassBill) {
        ApiService api = HttpMethod.getInstance().create(ApiService.class);
        api.savaReceiverJobs(receiveSaveJobPost)
                .compose(RxSchedulers.<BaseInfo<String>>compose(this))
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public void onSuccess(BaseInfo<String> baseInfo) {
                        if (baseInfo != null) {
                            Toast.makeText(PickCollectGoodsActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                            changeBtnShow(true);

                            if (isNeedPassBill)
                                suerReceiveJobs();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        Toast.makeText(PickCollectGoodsActivity.this, "保存失败" + msg, Toast.LENGTH_SHORT).show();
                    }
                });
    }

    /**
     * 过账
     */
    private void suerReceiveJobs() {
        ApiService api = HttpMethod.getInstance().create(ApiService.class);

        api.sureReceiverJobs(receiveJobDetail.getNoticeOrderId())
                .compose(RxSchedulers.<BaseInfo<String>>compose(this))
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public void onSuccess(BaseInfo<String> baseInfo) {
                        if (baseInfo != null)
                            batchPositionBtn.setText("已经过账");
                    }

                    @Override
                    public void onFailure(String msg) {
                    }
                });
    }


    private void changeBtnShow(boolean hasPost) {
        receiveSaveJobPost.setHasPost(hasPost);
        addNewPositionBtn.setText(receiveSaveJobPost.isHasPost() ? "已保存" : "保存");
        if (!hasPost)
            batchPositionBtn.setText("保存并过账");
    }

}


